package com.yuanduan.mplus.view;

import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.cj.framework.app.BaseActivity;
import com.cj.framework.util.HttpImp;
import com.cj.framework.util.IHttp;
import com.cj.framework.util.RSAUtils;
import com.cj.framework.widget.HorizontalBarChart;
import com.yuanduan.mplus.R;
import com.yuanduan.mplus.config.Config;
import com.yuanduan.mplus.model.DBManager;
import com.yuanduan.mplus.model.entity.User;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

/**
 * 废弃
 */
public class SafetyDriveDetailActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private ArrayList<DateInfo> dateInfos;
    private User user;
    private Calendar calendar=Calendar.getInstance();
    private HorizontalBarChart hbc_0;
    private HorizontalBarChart hbc_1;
    private HorizontalBarChart hbc_2;
    private HorizontalBarChart hbc_3;
    private HorizontalBarChart hbc_4;
    private HorizontalBarChart hbc_5;
    private HorizontalBarChart hbc_6;
    private HorizontalBarChart hbc_7;
    private HorizontalBarChart hbc_8;
    private HorizontalBarChart hbc_9;
    private ImageView iv_dian1;
    private ImageView iv_dian2;
    private ImageView iv_dian3;
    private ImageView iv_dian4;
    private ImageView iv_dian5;
    private ImageView iv_dian6;
    private ImageView iv_dian7;
    private ImageView iv_dian8;
    private ImageView iv_dian9;
    private ImageView iv_dian0;
    private LinearLayout ll_1;
    private LinearLayout ll_2;
    private LinearLayout ll_3;
    private LinearLayout ll_4;
    private LinearLayout ll_5;
    private LinearLayout ll_6;
    private LinearLayout ll_7;
    private LinearLayout ll_8;
    private LinearLayout ll_9;
    private LinearLayout ll_10;
    private ImageView iv_back;
    private RadioGroup rg;
    private TextView tv_title;
    private TextView tv_lookrecord;

    private int currentLevel=0;
    private HorizontalBarChart[] horizontalBarCharts;
    private ProgressDialog dialog;
    private String[] args={"LDW","HMW","SLI","FCW","UFCW","PCW","ACC","BRAKE","FLC","TB"};
    private ImageView[] dianzans;
    private IHttp iHttp=new HttpImp();
    private IHttp.HttpInfoCallbackWithIndex httpInfoCallbackWithIndex=new IHttp.HttpInfoCallbackWithIndex() {
        @Override
        public void onSucceed(Object result, int index) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    dialog.dismiss();
                }
            },500);
            if(TextUtils.isEmpty((CharSequence) result)){
                return;
            }
            String result1= RSAUtils.decryptWithRSA((String) result);
            JSONObject data=null;
            try {
                data=new JSONObject((String) result1);
                if(data.getInt("RS")!=1){
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            switch (index){
                case 0:
                    fillBardataset(data,0);
                    animalBar(bardataset0);
                    for(int i=0;i<bardataset0.length;i++){
                        if(bardataset0[i][0]<bardataset0[i][1]){
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan2));
                        }else{
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan));
                        }
                    }
                    break;
                case 1:
                    fillBardataset(data,1);
                    animalBar(bardataset1);
                    for(int i=0;i<bardataset1.length;i++){
                        if(bardataset1[i][0]<bardataset1[i][1]){
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan2));
                        }else{
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan));
                        }
                    }
                    break;
                case 2:
                    fillBardataset(data,2);
                    animalBar(bardataset2);
                    for(int i=0;i<bardataset2.length;i++){
                        if(bardataset2[i][0]<bardataset2[i][1]){
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan2));
                        }else{
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan));
                        }
                    }
                    break;
                case 3:
                    fillBardataset(data,3);
                    animalBar(bardataset3);
                    for(int i=0;i<bardataset3.length;i++){
                        if(bardataset3[i][0]<bardataset3[i][1]){
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan2));
                        }else{
                            dianzans[i].setImageBitmap(BitmapFactory.decodeResource(getResources(),R.mipmap.safetydetail_dianzan));
                        }
                    }
                    break;
            }
        }

        @Override
        public void onFailed(Object result, int index) {
            dialog.dismiss();
        }
    };

    @Override
    protected void findId() {
        iv_dian0= (ImageView) findViewById(R.id.safetydetail_dian1);
        iv_dian1= (ImageView) findViewById(R.id.safetydetail_dian2);
        iv_dian3= (ImageView) findViewById(R.id.safetydetail_dian4);
        iv_dian2= (ImageView) findViewById(R.id.safetydetail_dian3);
        iv_dian4= (ImageView) findViewById(R.id.safetydetail_dian5);
        iv_dian5= (ImageView) findViewById(R.id.safetydetail_dian6);
        iv_dian6= (ImageView) findViewById(R.id.safetydetail_dian7);
        iv_dian7= (ImageView) findViewById(R.id.safetydetail_dian8);
        iv_dian8= (ImageView) findViewById(R.id.safetydetail_dian9);
        iv_dian9= (ImageView) findViewById(R.id.safetydetail_dian10);
        tv_lookrecord= (TextView) findViewById(R.id.safetydetail_lookrecord);
        tv_title= (TextView) findViewById(R.id.safetydetail_title);
        rg= (RadioGroup) findViewById(R.id.safetydetail_rg);
        iv_back= (ImageView) findViewById(R.id.safetydetail_back);
        hbc_0= (HorizontalBarChart) findViewById(R.id.hbc_1);
        hbc_1= (HorizontalBarChart) findViewById(R.id.hbc_2);
        hbc_2= (HorizontalBarChart) findViewById(R.id.hbc_3);
        hbc_3= (HorizontalBarChart) findViewById(R.id.hbc_4);
        hbc_4= (HorizontalBarChart) findViewById(R.id.hbc_5);
        hbc_5= (HorizontalBarChart) findViewById(R.id.hbc_6);
        hbc_6= (HorizontalBarChart) findViewById(R.id.hbc_7);
        hbc_7= (HorizontalBarChart) findViewById(R.id.hbc_8);
        hbc_8= (HorizontalBarChart) findViewById(R.id.hbc_9);
        hbc_9= (HorizontalBarChart) findViewById(R.id.hbc_10);
        ll_1= (LinearLayout) findViewById(R.id.safetydetail_ll_1);
        ll_2= (LinearLayout) findViewById(R.id.safetydetail_ll_2);
        ll_3= (LinearLayout) findViewById(R.id.safetydetail_ll_3);
        ll_4= (LinearLayout) findViewById(R.id.safetydetail_ll_4);
        ll_5= (LinearLayout) findViewById(R.id.safetydetail_ll_5);
        ll_6= (LinearLayout) findViewById(R.id.safetydetail_ll_6);
        ll_7= (LinearLayout) findViewById(R.id.safetydetail_ll_7);
        ll_8= (LinearLayout) findViewById(R.id.safetydetail_ll_8);
        ll_9= (LinearLayout) findViewById(R.id.safetydetail_ll_9);
        ll_10= (LinearLayout) findViewById(R.id.safetydetail_ll_10);
    }

    @Override
    protected void logic() {
        dianzans=new ImageView[]{iv_dian0,iv_dian1,iv_dian2,iv_dian3,iv_dian4,iv_dian5,iv_dian6,iv_dian7,iv_dian8,iv_dian9};
        user= DBManager.getInstance(getApplicationContext()).getUser();
        dateInfos=getDateInfos();
        horizontalBarCharts= new HorizontalBarChart[]{hbc_0, hbc_1, hbc_2, hbc_3, hbc_4, hbc_5, hbc_6, hbc_7, hbc_8, hbc_9};
        dialog=new ProgressDialog(this);
        ll_1.setOnClickListener(this);
        ll_2.setOnClickListener(this);
        ll_3.setOnClickListener(this);
        ll_4.setOnClickListener(this);
        ll_5.setOnClickListener(this);
        ll_6.setOnClickListener(this);
        ll_7.setOnClickListener(this);
        ll_8.setOnClickListener(this);
        ll_9.setOnClickListener(this);
        ll_10.setOnClickListener(this);

        iv_back.setOnClickListener(this);
        tv_lookrecord.setOnClickListener(this);

        rg.check(R.id.safetydetail_rb_zhou1);
        ((RadioButton)rg.getChildAt(0)).setText(dateInfos.get(0).weekcode+"周(上周)");
        ((RadioButton)rg.getChildAt(1)).setText(dateInfos.get(1).weekcode+"周");
        ((RadioButton)rg.getChildAt(2)).setText(dateInfos.get(2).weekcode+"周");
        ((RadioButton)rg.getChildAt(3)).setText(dateInfos.get(3).weekcode+"周");
        rg.setOnCheckedChangeListener(this);
        if(Config.userType==1){
            //发送网络请求，请求周记录
            //显示进度条
            dialog.show();
            HashMap<String,String> hashMap=new HashMap<>();
            hashMap.put("UserPhone",user.getUserPhone());
            hashMap.put("YearCode",dateInfos.get(0).yearcode+"");
            hashMap.put("WeekCode",dateInfos.get(0).weekcode+"");
            iHttp.httpInfoPostWithIndex(Config.URL+Config.USERSECURITY,Config.translateMap(hashMap),httpInfoCallbackWithIndex,0);

        }else if(Config.userType==2){
            //修改title
            //显示进度条
            //发送网络请求，请求周记录
            Bundle bundle=getIntent().getExtras();
            tv_title.setText(bundle.getString("RealName")+"经济驾驶详情");
            dialog.show();
            HashMap<String,String> hashMap=new HashMap<>();
            hashMap.put("UserPhone",bundle.getString("UserPhone"));
            hashMap.put("YearCode",dateInfos.get(0).yearcode+"");
            hashMap.put("WeekCode",dateInfos.get(0).weekcode+"");
            iHttp.httpInfoPostWithIndex(Config.URL+Config.USERSECURITY,Config.translateMap(hashMap),httpInfoCallbackWithIndex,0);
        }else{
            //游客
        }
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_safety_drive_detail;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.safetydetail_back:
                finish();
                break;
            case R.id.safetydetail_lookrecord:
                //查看行车记录
                openActivity(DriveRecordActivity.class);
                break;
            case R.id.safetydetail_ll_1:
                Intent intent1=new Intent(this,ReportActivity.class);
                intent1.putExtra("reportType","偏道预警");
                intent1.putExtra("Choose","security");
                intent1.putExtra("Type","LDW");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent1.putExtras(bundle);
                }
                startActivity(intent1);
                break;
            case R.id.safetydetail_ll_2:
                Intent intent2=new Intent(this,ReportActivity.class);
                intent2.putExtra("reportType","车距预警");
                intent2.putExtra("Choose","security");
                intent2.putExtra("Type","HMW");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent2.putExtras(bundle);
                }
                startActivity(intent2);
                break;
            case R.id.safetydetail_ll_3:
                Intent intent3=new Intent(this,ReportActivity.class);
                intent3.putExtra("reportType","超速预警");
                intent3.putExtra("Choose","security");
                intent3.putExtra("Type","SLI");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent3.putExtras(bundle);
                }
                startActivity(intent3);
                break;
            case R.id.safetydetail_ll_4:
                Intent intent4=new Intent(this,ReportActivity.class);
                intent4.putExtra("reportType","碰撞预警");
                intent4.putExtra("Choose","security");
                intent4.putExtra("Type","FCW");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent4.putExtras(bundle);
                }
                startActivity(intent4);
                break;
            case R.id.safetydetail_ll_5:
                Intent intent5=new Intent(this,ReportActivity.class);
                intent5.putExtra("Choose","security");
                intent5.putExtra("reportType","低速预警");
                intent5.putExtra("Type","UFCW");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent5.putExtras(bundle);
                }
                startActivity(intent5);
                break;
            case R.id.safetydetail_ll_6:
                Intent intent6=new Intent(this,ReportActivity.class);
                intent6.putExtra("reportType","行人预警");
                intent6.putExtra("Choose","security");
                intent6.putExtra("Type","PCW");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent6.putExtras(bundle);
                }
                startActivity(intent6);
                break;
            case R.id.safetydetail_ll_7:
                Intent intent7=new Intent(this,ReportActivity.class);
                intent7.putExtra("reportType","加速预警");
                intent7.putExtra("Choose","security");
                intent7.putExtra("Type","ACC");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent7.putExtras(bundle);
                }
                startActivity(intent7);
                break;
            case R.id.safetydetail_ll_8:
                Intent intent8=new Intent(this,ReportActivity.class);
                intent8.putExtra("reportType","急刹预警");
                intent8.putExtra("Type","BRAKE");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent8.putExtras(bundle);
                }
                startActivity(intent8);
                break;
            case R.id.safetydetail_ll_9:
                Intent intent9=new Intent(this,ReportActivity.class);
                intent9.putExtra("reportType","快速变道");
                intent9.putExtra("Choose","security");
                intent9.putExtra("Type","FLC");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent9.putExtras(bundle);
                }
                startActivity(intent9);
                break;
            case R.id.safetydetail_ll_10:
                Intent intent10=new Intent(this,ReportActivity.class);
                intent10.putExtra("reportType","转弯急刹");
                intent10.putExtra("Choose","security");
                intent10.putExtra("Type","TB");
                if(Config.userType==2){
                    Bundle bundle=new Bundle();
                    bundle.putString("UserPhone",getIntent().getExtras().getString("UserPhone"));
                    bundle.putString("RealName",getIntent().getExtras().getString("RealName"));
                    intent10.putExtras(bundle);
                }
                startActivity(intent10);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        for(int i=0;i<horizontalBarCharts.length;i++){
            horizontalBarCharts[i].setLength(0,0,0);
        }
        switch (checkedId){
            case R.id.safetydetail_rb_zhou1:
                currentLevel=0;
                dialog.show();
                break;
            case R.id.safetydetail_rb_zhou2:
                currentLevel=1;
                dialog.show();
                break;
            case R.id.safetydetail_rb_zhou3:
                currentLevel=2;
                dialog.show();
                break;
            case R.id.safetydetail_rb_zhou4:
                currentLevel=3;
                dialog.show();
                break;
        }
        if(Config.userType==1){
            HashMap<String,String> hashMap=new HashMap<>();
            hashMap.put("UserPhone",user.getUserPhone());
            hashMap.put("YearCode",dateInfos.get(currentLevel).yearcode+"");
            hashMap.put("WeekCode",dateInfos.get(currentLevel).weekcode+"");
            iHttp.httpInfoPostWithIndex(Config.URL+Config.USERSECURITY,Config.translateMap(hashMap),httpInfoCallbackWithIndex,currentLevel);
        }else if(Config.userType==2){
            Bundle bundle=getIntent().getExtras();
            HashMap<String,String> hashMap=new HashMap<>();
            hashMap.put("UserPhone",bundle.getString("UserPhone"));
            hashMap.put("YearCode",dateInfos.get(currentLevel).yearcode+"");
            hashMap.put("WeekCode",dateInfos.get(currentLevel).weekcode+"");
            iHttp.httpInfoPostWithIndex(Config.URL+Config.USERSECURITY,Config.translateMap(hashMap),httpInfoCallbackWithIndex,currentLevel);
        }else{

        }
    }

    private int[][] bardataset0 =new int[10][3];
    private int[][] bardataset1 =new int[10][3];
    private int[][] bardataset2 =new int[10][3];
    private int[][] bardataset3 =new int[10][3];

    private void animalBar(int[][] datas){
        for(int i=0;i<datas.length;i++){
            int[] temp1=new int[3];
            for(int j=0;j<datas[i].length;j++){
                temp1[j]=datas[i][j];
            }
            horizontalBarCharts[i].setLength(temp1[0],temp1[1],temp1[2]);
        }
    }

    private void fillBardataset(JSONObject result,int index){
        if(result==null){
            return;
        }
        int[][] temp=new int[10][3];
        try {
            JSONObject ldw=result.getJSONObject("LDW");
            JSONObject hmw=result.getJSONObject("HMW");
            JSONObject sli=result.getJSONObject("SLI");
            JSONObject fcw=result.getJSONObject("FCW");
            JSONObject ufcw=result.getJSONObject("UFCW");
            JSONObject pcw=result.getJSONObject("PCW");
            JSONObject acc=result.getJSONObject("ACC");
            JSONObject brake=result.getJSONObject("BRAKE");
            JSONObject flc=result.getJSONObject("FLC");
            JSONObject tb=result.getJSONObject("TB");
            temp[0][0]=ldw.getInt("self");
            temp[0][1]=ldw.getInt("average");
            temp[0][2]=ldw.getInt("max");
            temp[1][0]=hmw.getInt("self");
            temp[1][1]=hmw.getInt("average");
            temp[1][2]=hmw.getInt("max");
            temp[2][0]=sli.getInt("self");
            temp[2][1]=sli.getInt("average");
            temp[2][2]=sli.getInt("max");
            temp[3][0]=fcw.getInt("self");
            temp[3][1]=fcw.getInt("average");
            temp[3][2]=fcw.getInt("max");
            temp[4][0]=ufcw.getInt("self");
            temp[4][1]=ufcw.getInt("average");
            temp[4][2]=ufcw.getInt("max");
            temp[5][0]=pcw.getInt("self");
            temp[5][1]=pcw.getInt("average");
            temp[5][2]=pcw.getInt("max");
            temp[6][0]=acc.getInt("self");
            temp[6][1]=acc.getInt("average");
            temp[6][2]=acc.getInt("max");
            temp[7][0]=brake.getInt("self");
            temp[7][1]=brake.getInt("average");
            temp[7][2]=brake.getInt("max");
            temp[8][0]=flc.getInt("self");
            temp[8][1]=flc.getInt("average");
            temp[8][2]=flc.getInt("max");
            temp[9][0]=tb.getInt("self");
            temp[9][1]=tb.getInt("average");
            temp[9][2]=tb.getInt("max");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        switch(index){
            case 0:
                bardataset0=temp;
                break;
            case 1:
                bardataset1=temp;
                break;
            case 2:
                bardataset2=temp;
                break;
            case 3:
                bardataset3=temp;
                break;
        }

    }

    public static ArrayList<DateInfo> getDateInfos(){
        ArrayList<DateInfo> dateInfos=new ArrayList<>();
        Calendar calendar=Calendar.getInstance();
        int thisweek=calendar.get(Calendar.WEEK_OF_YEAR);
        int thisyear=calendar.get(Calendar.YEAR);
        DateInfo dateInfo1=new DateInfo(),dateInfo2=new DateInfo(),dateInfo3=new DateInfo(),dateInfo4=new DateInfo();
        if(thisweek==1){
            dateInfo1.weekcode=52;
            dateInfo1.yearcode=thisyear-1;
            dateInfo2.weekcode=51;
            dateInfo2.yearcode=thisyear-1;
            dateInfo3.weekcode=50;
            dateInfo3.yearcode=thisyear-1;
            dateInfo4.weekcode=49;
            dateInfo4.yearcode=thisyear-1;
        }else
        if(thisweek==2){
            dateInfo1.weekcode=1;
            dateInfo1.yearcode=thisyear;
            dateInfo2.weekcode=52;
            dateInfo2.yearcode=thisyear-1;
            dateInfo3.weekcode=51;
            dateInfo3.yearcode=thisyear-1;
            dateInfo4.weekcode=50;
            dateInfo4.yearcode=thisyear-1;
        }else
        if(thisweek==3){
            dateInfo1.weekcode=2;
            dateInfo1.yearcode=thisyear;
            dateInfo2.weekcode=1;
            dateInfo2.yearcode=thisyear;
            dateInfo3.weekcode=52;
            dateInfo3.yearcode=thisyear-1;
            dateInfo4.weekcode=51;
            dateInfo4.yearcode=thisyear-1;
        }else
        if(thisweek==4){
            dateInfo1.weekcode=3;
            dateInfo1.yearcode=thisyear;
            dateInfo2.weekcode=2;
            dateInfo2.yearcode=thisyear;
            dateInfo3.weekcode=1;
            dateInfo3.yearcode=thisyear;
            dateInfo4.weekcode=52;
            dateInfo4.yearcode=thisyear-1;
        }else{
            dateInfo1.weekcode=thisweek-1;
            dateInfo1.yearcode=thisyear;
            dateInfo2.weekcode=thisweek-2;
            dateInfo2.yearcode=thisyear;
            dateInfo3.weekcode=thisweek-3;
            dateInfo3.yearcode=thisyear;
            dateInfo4.weekcode=thisweek-4;
            dateInfo4.yearcode=thisyear;
        }
        dateInfos.add(dateInfo1);
        dateInfos.add(dateInfo2);
        dateInfos.add(dateInfo3);
        dateInfos.add(dateInfo4);
        return dateInfos;
    }

    static class  DateInfo{
        int yearcode;
        int weekcode;
    }
}
